/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipccenter.toster.ejb;

import com.sun.xml.ws.api.tx.at.Transactional;
import edu.ipccenter.toster.Session;
import edu.ipccenter.toster.SessionUtils;
import edu.ipccenter.toster.User;
import edu.ipccenter.toster.UserUtils;
import edu.ipccenter.toster.jpa.JPASession;
import java.math.BigInteger;
import java.util.Date;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.Query;

/**
 *
 * @author 7
 */
@Stateless(name="SessionUtils")
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED )
public class SessionUtilsBean implements SessionUtils {
    @EJB
    UserUtils user_utils;

    @PersistenceContext(unitName="TosterPU")
        private EntityManager em;
    
    @Override
    public Session create(BigInteger userid) {
        Session session = null;
        
        if(user_utils.findByID(userid) == null) return null;
        User user;
        user = user_utils.findByID(userid);
        
        if(user.getRunTimeSession() == null){
        
          try {  
             InitialContext ic  = new InitialContext();
             session = (Session) ic.lookup("java:global/Tost/Tost-ejb/Session");
             
        } catch (NamingException ex) {
            Logger.getLogger(SessionUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
        }
          Date date  = new Date();
          
          JPASession jsession = new JPASession();
          
          jsession.setStart_date(date);
          jsession.setUser_id(userid);
          em.merge(jsession);
        
          session.setSessionData(jsession);
        }else{
            
           
            return user.getRunTimeSession();
            
        }  
        
        
          return session;
    }

    @Override
    public Session findById(BigInteger sessionid) {
         Session session = null;
         if(sessionid == null) return null;
         
         Query q = em.createQuery("select c from JPASession c where c.session_id =:id");
         q.setParameter("id", sessionid);
         
          JPASession res = null;
       try{
           res = (JPASession)q.getSingleResult();
           
       }catch(Exception e){
           return null;
       }
       
          try {  
             InitialContext ic  = new InitialContext();
             session = (Session) ic.lookup("java:global/Tost/Tost-ejb/Session");
             
        } catch (NamingException ex) {
            Logger.getLogger(SessionUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
        }
         session.setSessionData(res);
          
         return session;
         
    }

    @Override
    public Session findbyDate(Date date) {
       Session session = null;
       if(date == null) return null;
       
       Query q = em.createQuery("select c from JPASession c where c.start_date =:date");
       q.setParameter("date", date);
        JPASession res = null;
       try{
           res = (JPASession)q.getSingleResult();
           
       }catch(Exception e){
           return null;
       }
       
          try {  
             InitialContext ic  = new InitialContext();
             session = (Session) ic.lookup("java:global/Tost/Tost-ejb/Session");
             
        } catch (NamingException ex) {
            Logger.getLogger(SessionUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
        }
          
          session.setSessionData(res);
          return session;
    }
    
}
